import { getToken, setToken} from '@/utils/auth';
import api from "@/api/request";

const state = {
	token: getToken() || null,
	name: '测试人',
	avatar: '',
	introduction: '',
	roles: []
};

const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_INTRODUCTION: (state, introduction) => {
		state.introduction = introduction
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	},
	SET_ROLES: (state, roles) => {
		state.roles = roles
	}
};

const actions = {
	login({ commit }, userInfo) {
		const { account, password, validate, uuid } = userInfo;
		return new Promise((resolve, reject) => {
			api("member.loginUser", {
				account: account.trim(),
				password: password,
				validate: validate.toLowerCase(),
				uuid
			}, false).then((response) => {
				const {
					data
				} = response
				commit('SET_TOKEN', data)
				setToken(data)
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
